package com.helloandroid.activitys

import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.viewModels
import com.helloandroid.R
import com.helloandroid.activitys.fragments.MatchFragment1
import com.helloandroid.base.BaseActivity
import com.helloandroid.dialogs.TianTianHongBaoDialog
import com.helloandroid.models.HBMatchViewModel
import com.helloandroid.models.MatchData
import com.helloandroid.models.TTHBSData
import com.helloandroid.tools.MyLog
import com.helloandroid.vo.TTMatchInfo

class TianTianHongBaoActivity : BaseActivity() {

    val model: HBMatchViewModel by viewModels()

    private lateinit var mCurFragment: MatchFragment1
    var isReady = false

    // views
    lateinit var btnBig3000:LinearLayout
    lateinit var btnBig8000:LinearLayout
    lateinit var tvSmall3000:TextView
    lateinit var tvSmall8000:TextView

    // data
    var tthbsData:TTHBSData? = null

    override fun getContentViewResId() = R.layout.activity_tiantianhongbao

    override fun initView() {
        btnBig3000 = findViewById(R.id.btnBig3000)
        btnBig8000 = findViewById(R.id.btnBig8000)
        tvSmall3000 = findViewById(R.id.tvSmall3000)
        tvSmall8000 = findViewById(R.id.tvSmall8000)

        // init views
        btnBig3000.setOnClickListener{btn3000Clicked()}
        tvSmall3000.setOnClickListener { btn3000Clicked() }
        btnBig8000.setOnClickListener {btn8000Clicked()}
        tvSmall8000.setOnClickListener { btn8000Clicked() }

        // 返回
        findBtn(R.id.ibBack).setOnClickListener {
            finish()
        }

        // 显示活动规则
        findBtn(R.id.btnHelp).setOnClickListener {
            val dialog = TianTianHongBaoDialog(this);
            dialog.show();
        }
        mCurFragment = MatchFragment1(TTMatchInfo.M3000, this)
        supportFragmentManager.beginTransaction().replace(R.id.flFragmentContainer, mCurFragment).commit()
        showTab3000()
        showMatch(TTMatchInfo.M3000)
    }


    fun btn3000Clicked(){
        MyLog.info("btn3000Clicked called!")
        if (isReady){
            showMatch(TTMatchInfo.M3000)
        }
    }
    fun btn8000Clicked(){
        MyLog.info("btn8000Clicked called!")
        if (isReady){
            showMatch(TTMatchInfo.M8000)
        }
    }

    override fun initData() {
        model.getInfo { success, data ->
            isReady = success
            if (success) {
                tthbsData = data
                mCurFragment.updateUI( mCurFragment.matchType,getMatchData(mCurFragment.matchType))
            }
        }
    }

    private fun showMatch(matchType: Int) {
        if (mCurFragment.matchType == matchType) {
            return
        }
        when (matchType) {
            TTMatchInfo.M3000 -> {
                showTab3000()
                mCurFragment.updateUI(matchType, tthbsData!!.m3)
            }

            TTMatchInfo.M8000 -> {
                showTab8000()
                mCurFragment.updateUI(matchType, tthbsData!!.m8)
            }
        }
    }

    fun showTab3000(){
        tvSmall3000.visibility = View.INVISIBLE
        tvSmall8000.visibility = View.VISIBLE
        btnBig3000.visibility = View.VISIBLE
        btnBig8000.visibility = View.INVISIBLE
    }
    fun showTab8000(){
        tvSmall3000.visibility = View.VISIBLE
        tvSmall8000.visibility = View.INVISIBLE
        btnBig3000.visibility = View.INVISIBLE
        btnBig8000.visibility = View.VISIBLE
    }

    fun getMatchData(matchType: Int):MatchData{
        return if (matchType == TTMatchInfo.M3000){
            tthbsData!!.m3
        }else{
            tthbsData!!.m8
        }
    }
}